package commodity

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/commodity"
	commodityReq "github.com/flipped-aurora/gin-vue-admin/server/model/commodity/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CommodityApi struct {
}

var commodityService = service.ServiceGroupApp.CommodityServiceGroup.CommodityService

// CreateCommodity 创建Commodity
// @Tags Commodity
// @Summary 创建Commodity
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body commodity.Commodity true "创建Commodity"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /commodity/createCommodity [post]
func (commodityApi *CommodityApi) CreateCommodity(c *gin.Context) {
	var commodity commodity.Commodity
	_ = c.ShouldBindJSON(&commodity)

	temp, _ := c.Get("userID")
	key := temp.(uint)
	temp1 := int(key)
	commodity.Commodity_user = &temp1

	if err := commodityService.CreateCommodity(commodity); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteCommodity 删除Commodity
// @Tags Commodity
// @Summary 删除Commodity
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body commodity.Commodity true "删除Commodity"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /commodity/deleteCommodity [delete]
func (commodityApi *CommodityApi) DeleteCommodity(c *gin.Context) {
	var commodity commodity.Commodity
	_ = c.ShouldBindJSON(&commodity)
	if err := commodityService.DeleteCommodity(commodity); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteCommodityByIds 批量删除Commodity
// @Tags Commodity
// @Summary 批量删除Commodity
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Commodity"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /commodity/deleteCommodityByIds [delete]
func (commodityApi *CommodityApi) DeleteCommodityByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := commodityService.DeleteCommodityByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateCommodity 更新Commodity
// @Tags Commodity
// @Summary 更新Commodity
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body commodity.Commodity true "更新Commodity"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /commodity/updateCommodity [put]
func (commodityApi *CommodityApi) UpdateCommodity(c *gin.Context) {
	var commodity commodity.Commodity
	_ = c.ShouldBindJSON(&commodity)

	temp, _ := c.Get("userID")
	key := temp.(uint)
	temp1 := int(key)
	commodity.Commodity_user = &temp1

	if err := commodityService.UpdateCommodity(commodity); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindCommodity 用id查询Commodity
// @Tags Commodity
// @Summary 用id查询Commodity
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query commodity.Commodity true "用id查询Commodity"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /commodity/findCommodity [get]
func (commodityApi *CommodityApi) FindCommodity(c *gin.Context) {
	var commodity commodity.Commodity
	_ = c.ShouldBindQuery(&commodity)
	if recommodity, err := commodityService.GetCommodity(commodity.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"recommodity": recommodity}, c)
	}
}

// GetCommodityList 分页获取Commodity列表
// @Tags Commodity
// @Summary 分页获取Commodity列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query commodityReq.CommoditySearch true "分页获取Commodity列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /commodity/getCommodityList [get]
func (commodityApi *CommodityApi) GetCommodityList(c *gin.Context) {
	var pageInfo commodityReq.CommoditySearch
	_ = c.ShouldBindQuery(&pageInfo)
	if list, total, err := commodityService.GetCommodityInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// Commodity 分页获取Commodity列表
// @Tags Commodity
// @Router /commodity/ [get]
func (commodityApi *CommodityApi) Commodity(c *gin.Context) { //commodity_id为零表示获取全部

}

// CommodityCreate 创建Commodity
// @Tags Commodity
// @Router /commodity/create [post]
func (commodityApi *CommodityApi) CommodityCreate(c *gin.Context) {

}

// CommodityDelete 删除Commodity
// @Tags Commodity
// @Router /commodity/delete [post]
func (commodityApi *CommodityApi) CommodityDelete(c *gin.Context) {

}

// CommodityUpdate 删除Commodity
// @Tags Commodity
// @Router /commodity/update [update]
func (commodityApi *CommodityApi) CommodityUpdate(c *gin.Context) {

}

// CommodityList 展示Commodity
// @Tags Commodity
// @Router /commodity/list [get]
func (commodityApi *CommodityApi) CommodityList(c *gin.Context) {

}
